package com.hulk.dryad.manage.framework.websocket.server.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hulk.dryad.common.constant.enums.DelFlag;
import com.hulk.dryad.common.constant.enums.LockFlag;
import com.hulk.dryad.common.constant.enums.OnlineFlag;
import com.hulk.dryad.manage.framework.datasource.DynamicDataSourceHolder;
import com.hulk.dryad.persistence.entity.SysUserModel;
import com.hulk.dryad.persistence.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.util.List;


/**
 * redis 实现
 *
 * @author hulk
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "websocket.server.userStore.name", havingValue = "db")
@RequiredArgsConstructor
public class DbWsUserStore implements IWsUserStore {

	private final SysUserMapper sysUserMapper;

	@Override
	public List<String> listAll(String tenant) {

		LocalDateTime currentTime = LocalDateTime.now();
		LambdaQueryWrapper<SysUserModel> qw = Wrappers.<SysUserModel>lambdaQuery()
				.eq(SysUserModel::getDelFlag,  DelFlag.N.getValue())
				.eq(SysUserModel::getLockFlag, LockFlag.N.getValue())
				.eq(SysUserModel::getOnlineFlag,OnlineFlag.Y.getValue())
				.gt(SysUserModel::getTokenExpireTime, currentTime);
		DynamicDataSourceHolder.set(tenant);
		List list =  sysUserMapper.selectList(qw);
		return list;
	}

	@Override
	public boolean has(String userInfo) {

		String tenant =  pickTenant(userInfo);
		String name = pickName(userInfo);

		LocalDateTime currentTime = LocalDateTime.now();
		LambdaQueryWrapper<SysUserModel> qw = Wrappers.<SysUserModel>lambdaQuery()
				.eq(SysUserModel::getDelFlag,  DelFlag.N.getValue())
				.eq(SysUserModel::getLockFlag, LockFlag.N.getValue())
				.eq(SysUserModel::getOnlineFlag,OnlineFlag.Y.getValue())
				.eq(SysUserModel::getUsername, name)
				.gt(SysUserModel::getTokenExpireTime, currentTime);
		DynamicDataSourceHolder.set(tenant);
		SysUserModel model = sysUserMapper.selectOne(qw);
		return model==null ? false : true;
	}

	@Override
	public void put(String userInfo) {
		String tenant =  pickTenant(userInfo);
		String name = pickName(userInfo);
		LocalDateTime currentTime = LocalDateTime.now();
		LambdaUpdateWrapper<SysUserModel> uw = Wrappers.<SysUserModel>lambdaUpdate()
				.eq(SysUserModel::getDelFlag,  DelFlag.N.getValue())
				.eq(SysUserModel::getLockFlag, LockFlag.N.getValue())
				.eq(SysUserModel::getUsername, name)
				.gt(SysUserModel::getTokenExpireTime, currentTime)
				.set(SysUserModel::getOnlineFlag,OnlineFlag.Y.getValue());
		DynamicDataSourceHolder.set(tenant);
		sysUserMapper.update(null,uw);
	}

	@Override
	public void remove(String userInfo) {
		String tenant =  pickTenant(userInfo);
		String name = pickName(userInfo);
		LambdaUpdateWrapper<SysUserModel> uw = Wrappers.<SysUserModel>lambdaUpdate()
				.eq(SysUserModel::getUsername, name)
				.set(SysUserModel::getOnlineFlag,OnlineFlag.N.getValue());
				//刷新会退出
				//.set(SysUserModel::getToken, StrUtil.uuid());
		DynamicDataSourceHolder.set(tenant);
		sysUserMapper.update(null,uw);

	}


}
